package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.UserInfo;
import com.example.prison.service.UserInfoService;
import com.example.prison.mapper.UserInfoMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author jiang
* @description 针对表【user_info】的数据库操作Service实现
* @createDate 2023-05-26 11:28:10
*/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
    implements UserInfoService{

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public PageInfo<UserInfo> get(int pageNumber, int pageSize, String userName) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(userInfoMapper.get(userName));
    }

    @Override
    public int add(UserInfo user) {
        String name = user.getUserName();
        if (ObjectUtils.isNotEmpty(userInfoMapper.getOneByName(name))) {
            return -1;
        } else {
            userInfoMapper.add(user);
            return 1;
        }
    }

    @Override
    public void delete(Integer[] ids) {
        userInfoMapper.deleteUser(ids);
    }

    @Override
    public int update(UserInfo user, Integer id) {
        UserInfo userDTO = userInfoMapper.getById(id);
        userInfoMapper.updateUser(user, id);
        List<UserInfo> userList = userInfoMapper.getBySameName(user.getUserName());
        if(userList.size() > 1) {
            userInfoMapper.updateUserFail(userDTO, id);
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public UserInfo getById(Integer id) {
        return userInfoMapper.getById(id);
    }

    @Override
    public PageInfo<UserInfo> getByName(int pageNumber, int pageSize, String name) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(userInfoMapper.getByName(name));
    }

    @Override
    public UserInfo getOneByName(String name) {
        return userInfoMapper.getOneByName(name);
    }


}




